$(function () {
    var loading = false;
    var url = '/person/showBrowseHistory?token='+localStorage.getItem('token');
    //判断当前访问的是收藏、浏览记录还是我的发布
    var type = getQueryString("type");
    //目前收藏的帖子数
    var maxItems = 999;
    //一页允许返回的最大条数
    var pageSize = 6;
    //页码
    var pageNum = 1;
    var backgroundurl = 'http://gqianniu.alicdn.com/bao/uploaded/i4//tfscom/i3/TB10LfcHFXXXXXKXpXXXXXXXXXX_!!0-item_pic.jpg_250x250q60.jpg';

    var flag = false;
    //判断访问的是哪个页面
    if (type=='1'){
        $('#headline').html('我的发布');
        $('#subtitle').html('我的发布');
    } else if (type=='2') {
        $('#headline').html('我的收藏');
        $('#subtitle').html('收藏');
    } else if (type== '3') {
        $('#headline').html('我的足迹');
        $('#subtitle').html('浏览记录');
    }

    //预先加载5条帖子信息
    addPersonCollection(pageSize,pageNum);

    function addPersonCollection(pageSize, pageIndex) {
        //拼接出查询的url
        var showUrl = url+"&pageSize="+pageSize+"&pageIndex="+pageIndex+"&type="+type;
        loading = true;
        //访问后台获取相应查询条件下的收藏列表
        $.getJSON(showUrl,function (data) {
            console.log(data);
            if (data.success){
                var html = '';
                if (type == 3) {
                    //浏览足迹
                    data.data.map(function (item,index) {
                        var time = new Date(item.lastEditTime).Format("hh");
                        var formats = time<=12&&time>=0?'上午':'下午';
                        html += '<div class="card demo-card-header-pic" data-topic-id="'+item.topic.topicId+'">'+
                            '<div style="background-image:url('+backgroundurl+')" valign="bottom" class="card-header color-white no-border">'+
                            item.personInfo.username+'</div>'+'<div class="card-content">'+
                            '<div class="card-content-inner">'+'<p class="color-gray">'+'浏览于&nbsp;'+
                            new Date(item.lastEditTime).Format("yyyy-MM-dd "+formats+"hh:mm")+'</p>'+
                            '<p>'+item.topic.topicName+'</p>'+'</div></div></div>';
                    })
                }else if (type == 1 || type == 2) {
                    //发布和收藏
                    data.data.map(function (item,index) {
                        html += '<div class="card demo-card-header-pic" data-topic-id="'+item.topicId+'">'+
                            '<div style="background-image:url('+backgroundurl+')" valign="bottom" class="card-header color-white no-border">'+'</div>'+'<div class="card-content">'+
                            '<div class="card-content-inner">'+'<p class="color-gray">'+'发表于&nbsp;'+
                            new Date(item.lastEditTime).Format("yyyy-MM-dd")+'</p>'+
                            '<p>'+item.topicName+'</p>'+'</div></div></div>';
                    });
                }
                maxItems = data.count;
                //将卡片集合添加到div中
                $('.list-div').append(html);
                //获取目前为止获取的卡片总数
                var total = $('.list-div .card').length;
                //若总数达到跟按照此条件查询列出来的总数一致,则停止后天的加载
                if(total>=maxItems){
                    $('.infinite-scroll-preloader').hide();
                }else {
                    $('.infinite-scroll-preloader').show();
                }
                //否则页码加1
                pageNum += 1;
                //加载结束,可以再次加载
                loading = false;
                //刷新页面,显示新加载的页面
                $.refreshScroller();
            }
        });
    }

    //下滑屏幕自动进行分页搜索
    $(document).on('infinite','.infinite-scroll-bottom',function () {
        if (loading) {
            return;
        }
        addPersonCollection(pageSize,pageNum);
    });

    $('.topic-list').on('click','.card',function (e) {
        var topicId = e.currentTarget.dataset.topicId;
        window.location.href = '/topic/topicContent?topicId='+topicId;
    });

    //初始化页面
    $.init();
});























